﻿namespace NTLib.Pipe.Streaming.Descriptors
{
    using Core.Descriptors;
    using Pipe.Descriptors;
    using System;

    /// <summary>
    /// Define the base information needed to managed a streaming source
    /// </summary>
    /// <seealso cref="NTLib.Pipe.Descriptors.BaseTargetDescription" />
    public class StreamingTargetDescriptor : BaseTargetDescription, IStreamingTargetDescriptor
    {
        #region Ctor

        /// <summary>
        /// Initializes a new instance of the <see cref="StreamingTargetDescriptor"/> class.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <param name="name">The name.</param>
        public StreamingTargetDescriptor(Guid id, IDescriptor formater, string name = null) 
            : base(id, name)
        {
            this.PackageFormater = formater;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets the formater.
        /// </summary>
        public IDescriptor PackageFormater
        {
            get;
        }

        #endregion

        #region Methods

        /// <summary>
        /// Creates the new clone instance.
        /// </summary>
        protected override IDescriptor CreateCloneNewInstance()
        {
            return new StreamingTargetDescriptor(this.Id, PackageFormater, Name);
        }

        /// <summary>
        /// Called when the <see cref="M:NTLib.Pipe.Descriptors.BaseTargetDescription.Equals(NTLib.Pipe.Descriptors.ITargetDescriptor)" /> method is called.
        /// All the basic check have already been check when the <see cref="M:NTLib.Pipe.Descriptors.BaseTargetDescription.OnEquals(NTLib.Pipe.Descriptors.ITargetDescriptor)" />
        /// is called.
        /// </summary>
        /// <param name="other"></param>
        protected override bool OnEquals(ITargetDescriptor other)
        {
            StreamingTargetDescriptor stream = other as StreamingTargetDescriptor;

            if (this.PackageFormater == null && stream.PackageFormater == null)
            {
                return true;
            }
            if (this.PackageFormater == null || stream.PackageFormater == null)
            {
                return false;
            }
            return base.OnEquals(other) &&
                   stream.PackageFormater.Equals(this.PackageFormater);
        }

        #endregion
    }
}
